<template>
	<view class="box-wp">
		<couponItem :model="voucherInfo"></couponItem>
		<view class="to-use" v-if="hadSumbit" @click="gotoHome">
			去使用 >
		</view>
		<view class="bottom-box">
			<u-button :throttleTime="800" v-if="hadSumbit" :disabled="true" :custom-style="{
					  background: '#276aee',
					  color: '#fff',
					  width: '682rpx',
					  border:'none'
					}">已领取</u-button>
			<u-button :throttleTime="800" v-else @click="submit" :custom-style="{
				  background: '#276aee',
				  color: '#fff',
				  width: '682rpx',
				  border:'none'
				}">领取</u-button>


		</view>
	</view>
</template>

<script>
	import couponItem from '@/pagesMall/components/couponItem/couponItem.vue'
	export default {
		components: {
			couponItem
		},
		data() {
			return {
				id: '',
				voucherInfo: {},
				openId: '',
				hadSumbit: false
			};
		},
		onLoad(opt) {
			console.log(opt);
			if (opt.id) {
				this.id = opt.id
				this.getVoucherInfo(opt.id)
			}

		},
		onShow() {
			this.getOpenId()
		},
		methods: {
			async gotoHome() {
				let hasCard = await this.queryCurrentUserInfoByOpenId()
				console.log(hasCard);
				if (hasCard) {
					uni.navigateTo({
						url: '/pagesMall/mallCenter/mallCenter'
					})
				} else {
					uni.switchTab({
						url: '/pages/card/card'
					})
				}
			},
			async getVoucherInfo() {
				let res = await this.$api.getVoucherInfo({
					id: this.id,
				})
				if (res.success && res.code == 200) {
					console.log(res.result, 'getVoucherInfo');
					this.voucherInfo = res.result
					if (res.result.userOpenId) {
						this.hadSumbit = true
						uni.showToast({
							title: '该代金券已被使用',
							icon: 'none'
						})
					}

				} else {
					this.voucherInfo = {}
					uni.showToast({
						icon: 'none',
						title: res.message
					})
				}
			},
			async submit() {
				let payload = {
					id: this.id,
					openId: this.openId
				}
				const {
					success,
					message,
					code
				} = await this.$api.handleVoucher(payload)
				console.log(payload, 'payload');
				if (code == 200 && success) {
					uni.showToast({
						title: '领取成功'
					})
					this.hadSumbit = true
				} else {
					uni.showToast({
						title: message,
						icon: 'none'
					});
				}
			},
			//获取openId
			async getOpenId() {
				uni.login({
					provider: 'weixin',
					onlyAuthorize: true,
					success: async (loginRes) => {
						let code = loginRes.code
						let payload = {
							code
						}
						let res = await this.$api.getOpenIdByCode(payload);
						if (res) {
							this.openId = res.openid
							// console.log('获取openId---', res);
							uni.setStorageSync('openId', res)
						}
					}
				});
			},
			//通过自己的openId查询名片信息
			async queryCurrentUserInfoByOpenId() {
				let res = await this.$api.queryCurrentCardByOpenId({
					openId: this.openId
				})
				if (res.result.id) {
					return res.result.id
				} else {
					return null
				}
			},
		},
	}
</script>
<style lang="scss" scoped>
	.to-use {
		padding: 30rpx 0;
		text-align: center;
		height: 40rpx;
		font-size: 30rpx;
		font-weight: 400;
		color: #276AEE;
		line-height: 39rpx;

	}
</style>